//
//  TLS_CommunityPageVC.m
//  Travel_World
//
//  Created by lanou on 15/10/12.
//  Copyright © 2015年 TLS. All rights reserved.
//

#import "TLS_CommunityPageVC.h"
#import "QTAFNnetWorkTool.h"
#import "TLS_TcomunityCell.h"
#import "TLS_TcoumnityModel.h"
#import "TLST_DetailVC.h"

@interface TLS_CommunityPageVC ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *comunityTabelView;
@property (nonatomic, strong) NSMutableArray *comunityTitleArr;
@property (nonatomic, strong) NSMutableArray *comunitySectionArr;
@end

@implementation TLS_CommunityPageVC
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.comunityTitleArr = [NSMutableArray array];
        self.comunitySectionArr = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"社区";
    self.navigationController.navigationBar.translucent = NO;
    
    self.comunityTabelView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.comunityTabelView.delegate = self;
    self.comunityTabelView.dataSource = self;
    self.comunityTabelView.rowHeight = 80;
    [self.view addSubview:self.comunityTabelView];
    [self.comunityTabelView mas_makeConstraints:^(MASConstraintMaker *make) {
        //make.right.left.top.bottom.equalTo(self.view);
        make.edges.equalTo(self.view);
    }];
    
    [self createData];
}

- (void)createData
{
    [QTAFNnetWorkTool getUrl:@"http://open.qyer.com/qyer/bbs/forum_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88262860611146&lon=121.5396766802357&page=1&track_app_channel=App%2520Store&track_app_version=6.5.5&track_device_info=iPhone7%2C2&track_deviceid=5B302CA5-E537-4B10-955D-520F4C91BAFE&track_os=ios%25208.1.2&v=1%20" body:nil hudSuperView:self.comunityTabelView result:QTJSON headerFile:nil success:^(id result) {
        for (NSDictionary *tempDic in result[@"data"]) {
            [self.comunityTitleArr addObject:tempDic[@"name"]];
            NSMutableArray *modelArr = [TLS_TcoumnityModel baseModelByArr:tempDic[@"group"]];
            [self.comunitySectionArr addObject:modelArr];
        }
        self.placeLabel.hidden = YES;
        [self.comunityTabelView reloadData];
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.comunitySectionArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *modelArr = self.comunitySectionArr[section];
    return modelArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TLS_TcomunityCell *cell = [TLS_TcomunityCell coumnityCellWithTableView:tableView];
    NSArray *tempArr = self.comunitySectionArr[indexPath.section];
    if (tempArr.count > indexPath.row) {
        cell.coumentyModel = tempArr[indexPath.row];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TLST_DetailVC *detailVC = [[TLST_DetailVC alloc] init];
    NSArray *tempArr = self.comunitySectionArr[indexPath.section];
    if (tempArr.count > indexPath.row) {
        TLS_TcoumnityModel *coumnityMdoel = tempArr[indexPath.row];
        detailVC.tcoumityModel = coumnityMdoel;
        detailVC.myId = coumnityMdoel.tqyId;
    }
    [self.navigationController pushViewController:detailVC animated:YES];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return self.comunityTitleArr[section];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
